Imports cmplib01
Imports System.Drawing
Imports System.Drawing.Drawing2D


Public Class frmUnitSelection
    '// creates a list of units to include. 

    Private _allUnits As New Units
    Private _biggestSize As Integer
    Private _fastestSpeed As Integer
    Private _biggestZoc As Double

    Private _tPV As Single

    Private ptrCurrentGame As CampaignerGameData

    Public Overloads Sub ShowDialog(ByRef game As CampaignerGameData)
        ptrCurrentGame = game
        PopulateUnitTypes()
        MyBase.ShowDialog()
    End Sub

    Public ReadOnly Property AllUnits() As Units
        Get
            Return _allUnits
        End Get
    End Property


    Private Sub PopulateUnitTypes()
        Dim unit As UnitType
        Dim index As Int16

        Try
            tvwUnitTypes.Nodes.Clear()

            For Each unit In ptrCurrentGame.UnitTypes
                ' add the unit's icon to the imagelist
                Debug.WriteLine(unit.Name)

                imlUnitIcons.Images.Add(unit.Icon(cmplib01.IconImageIndex_cgd.Owner))
                Dim nodUnit As New CampaignerItemTreeNode

                If unit.Size > _biggestSize Then _biggestSize = unit.Size
                If unit.MovementSpeed > _fastestSpeed Then _fastestSpeed = unit.MovementSpeed
                If unit.ZOCRange > _biggestZoc Then _biggestZoc = unit.ZOCRange

                nodUnit.Text = unit.TypeName
                nodUnit.Tag = unit.Key
                nodUnit.ImageIndex = index
                nodUnit.SelectedImageIndex = index
                nodUnit.ItemType = CampaignerItem_cgd.Unit
                nodUnit.Item = unit
                tvwUnitTypes.Nodes.Add(nodUnit)
                index += 1
            Next

            tvwUnitTypes.Sort()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub tvwUnitTypes_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwUnitTypes.AfterSelect

        If tvwUnitTypes.SelectedNode Is Nothing Then Exit Sub

        Dim unit As UnitType = DirectCast(tvwUnitTypes.SelectedNode, CampaignerItemTreeNode).Item

        txtUnitTypeDescription.Text = unit.Description
        numPV.Value = unit.SuggestedPointValue

        showSelectedStatistics(unit)

    End Sub

    Private Sub showSelectedStatistics(ByRef unit As UnitType)
        Dim strength As Single
        Try
            strength = unit.MovementSpeed / _fastestSpeed
            drawBar(strength, picSpeed, "Base Movement Speed")
            strength = unit.Size / _biggestSize
            drawBar(strength, picSize, "Size")
            strength = unit.ZOCRange / _biggestZoc
            drawBar(strength, picZOC, "Zone Of Control")
        Catch ex As Exception

        End Try

    End Sub

    Private Sub drawBar(ByVal strength As Single, ByVal target As PictureBox, ByVal text As String)

        Dim bar As New RectangleF(1, 1, (picSize.Width * strength) - 6, 14)

        Dim imCom As Image = New Bitmap(target.Width, target.Height)
        Dim g As Graphics = Graphics.FromImage(imCom)

        Dim br As New LinearGradientBrush(bar, Color.Red, Color.Green, LinearGradientMode.Horizontal)

        g.FillRectangle(br, bar)

        g.DrawString(text, Me.Font, New SolidBrush(Color.Yellow), 2, 2)

        target.Image = imCom

        g.Dispose()

    End Sub

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If tvwUnitTypes.SelectedNode Is Nothing Then Exit Sub

        Dim un As UnitType
        un = DirectCast(tvwUnitTypes.SelectedNode, CampaignerItemTreeNode).Item
        Dim nunit As UnitType
        nunit = un.Clone

        nunit.InitialPointValue = numPV.Value

        lstUnits.Items.Add(nunit)

        _tPV += numPV.Value
        lblTotalPV.Text = _tPV

    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        '// all we have to do is keep the collection empty.
        _allUnits = New Units
        Me.Close()

    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        '// here we create our little group of jonses. Schweeeet.
        _allUnits = New Units

        Dim un As UnitType
        For Each un In lstUnits.Items
            _allUnits.Add(un)
        Next
        Me.Hide()

    End Sub

    Private Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        If lstUnits.SelectedItem Is Nothing Then Exit Sub

        _tPV -= DirectCast(lstUnits.SelectedItem, UnitType).InitialPointValue
        lstUnits.Items.Remove(lstUnits.SelectedItem)
        lblTotalPV.Text = _tPV

    End Sub

    Public ReadOnly Property TotalPV() As Integer
        Get
            Return _tPV
        End Get
    End Property


End Class